using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Pharmacy.Ultilities;

namespace DIoms.Utilities
{
	/// <summary>
	/// Summary description for ValidateUtil.
	/// </summary>
	public class ValidateUtil
	{
        private const string PATTERN = @"[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}";

        /// <summary>
        /// Check if the C# decimal value > SQL decimal value
        /// </summary>
        /// <param name="dValue"></param>
        /// <param name="iPrecision"></param>
        /// <param name="iScale"></param>
        /// <returns></returns>
        /// <author></author>
        /// 2011.02.11 - ThangTran: renamed from ValidateDBDecimal() to ValidateDBDecimal().
        public static bool ValidateDBDecimal(decimal dValue, int iPrecision, int iScale)
        {
            try
            {
                string strAmount = dValue.ToString();
                string strAmountPrecision = strAmount;

                if (strAmount.Contains("."))
                {
                    strAmountPrecision = strAmount.Substring(0, strAmount.IndexOf('.'));
                }

                if (strAmountPrecision.Length > (iPrecision - iScale))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                //NotifyUserUtil.ShowException(ex);
                return true;
            }
        }

        /// <summary>
        /// Check if the C# decimal value > SQL decimal value
        /// </summary>
        /// <param name="dValue"></param>
        /// <author></author>
        /// <history>
        /// 2011.02.11 - ThangTran: redirected to ValidateDBDecimal(decimal, int, int).
        /// </history>
        public static bool ValidateDBDecimal(decimal dValue)
        {
            return ValidateDBDecimal(dValue, Constant.iDecimalPrecision, Constant.iDecimalScale);
        }

		/// <summary>
		/// Search value in list
		/// </summary>
		/// <param name="searchValue">Key Input</param>
		/// <param name="searchArray">List of Item</param>
		/// <returns>true if key exist in list of Item</returns>
		public static bool ValueIn(object searchValue, object[] searchArray)
		{
			for (int i=0;i<searchArray.Length;i++)
			{
				if (searchArray[i].Equals(searchValue))
				{
					return true;
				}
			}
			return false;
		}

		/// <summary>
		/// Check string is number
		/// </summary>
		/// <param name="Value">String Input</param>
		/// <returns>true if string oly contains number</returns>
		public static bool IsNumeric(string value1)
		{
			bool result = true;
			try
			{
				decimal.Parse(value1);
			}
			catch 
			{
				result = false;
			}
			return result;
		}

		/// <summary>
		/// Check string is Integer
		/// </summary>
		/// <param name="Value">string Input</param>
		/// <returns>true if Input is Integer</returns>
		public static bool IsInteger(string value1)
		{
			try
			{
				int.Parse(value1);
			}
			catch 
			{
				return false;
			}
			return true;
		}

		/// <summary>
		/// Check string is datetime
		/// </summary>
		/// <param name="Value">String Input</param>
		/// <param name="Format">Format of string</param>
		/// <returns>true if string is DateTime</returns>
		public static bool IsDateTime(string value1, string format)
		{
			bool result = true;
			try
			{
				DateTime.ParseExact(value1, format, null);
			}
			catch 
			{
				result = false;
			}
			return result;
		}
		/// <summary>
		/// Check object is valid
		/// </summary>
		/// <param name="o">object input</param>
		/// <returns>true if objec is valid</returns>

		public static bool IsObjectValid(object o)
		{
			if (o == null)
			{
				return false;
			}
			else
			{
				if (Convert.IsDBNull(o))
				{
					return false;
				}
				if (o.ToString().Trim().Length == 0)
				{
					return false;
				}
			}
			return true;
		}

        /// <summary>
        /// Check an e-mail string is valid
        /// </summary>
        /// <param name="emails">E-mail string (ex: "name1@abc.com, name2@abc.com")</param>
        /// <returns>false if one of emails is invalid</returns>
        public static bool ValidateEmails(string emails)
        {
            bool result = true;

            string[] splitedStrings = emails.Split(new char[] { ',', ';', ' ' });
            if (splitedStrings.Length <= 0)
            {
                result = false;
            }
            else
            {
                List<string> emailList = new List<string>();
                foreach (string str in splitedStrings)
                {
                    if (str != string.Empty)
                        emailList.Add(str);
                }

                result = (emailList.Count != 0 ? ValidateEmail(emailList) : false);
            }

            return result;
        }

        private static bool ValidateEmail(List<string> emails)
        {
            Regex matchRegex = new Regex(PATTERN);

            for (int i = 0; i < emails.Count; i++)
            {
                MatchCollection matches = matchRegex.Matches(emails[i]);
                if (matches.Count == 0)
                {
                    return false;
                }
            }

            return true;
        }
	}
}
